package org.mspring.mlog.web.module.web.ucenter;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.mspring.mlog.entity.Twitter;
import org.mspring.mlog.entity.TwitterReply;
import org.mspring.mlog.service.AttachmentService;
import org.mspring.mlog.service.TwitterReplyService;
import org.mspring.mlog.service.TwitterService;
import org.mspring.mlog.web.query.TwitterQueryCriterion;
import org.mspring.platform.dao.support.Page;
import org.mspring.platform.dao.support.Sort;
import org.mspring.platform.utils.RequestUtils;
import org.mspring.platform.web.ResponseEntity;
import org.mspring.platform.web.freemarker.widget.stereotype.Widget;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 
 * @author Gao Youbo
 * @since 2013年7月18日
 */
@Widget
@RequestMapping({ "/u/{username}/twitter", "/u/{username}/t" })
public class UCenter_TwitterWidget extends UCenter_AbstractWidget {
    @Autowired
    private TwitterService twitterService;
    @Autowired
    private TwitterReplyService twitterReplyService;
    @Autowired
    private AttachmentService attachmentService;

    @RequestMapping({ "/", "" })
    public String list(@ModelAttribute Page<Twitter> twitterPage, @ModelAttribute Twitter twitter, @RequestParam Map<String, Object> queryParams, HttpServletRequest request, HttpServletResponse response, Model model) {
        if (twitterPage == null) {
            twitterPage = new Page<Twitter>();
        }
        twitterPage.setSort(new Sort("id", Sort.DESC));

        if (twitter == null) {
            twitter = new Twitter();
        }
        queryParams.put("author.id", spaceUser.getId());
        twitterPage = twitterService.listPage(new TwitterQueryCriterion(queryParams), twitterPage);
        model.addAttribute("twitterPage", twitterPage);
        return "skin:/ucenter/twitter";
    }

    @RequestMapping("/replylist")
    @ResponseBody
    public ResponseEntity getReply(HttpServletRequest request, HttpServletResponse response, Model model) {
        ResponseEntity rsp = new ResponseEntity();
        Long twitter = RequestUtils.getRequestParameterAsLong(request, "twitter");
        if (twitter == null) {
            rsp.setSuccess(false);
            rsp.setMessage("无法判定Twitter对象");
            return rsp;
        }
        List<TwitterReply> replys = twitterReplyService.getTwitterReplyList(twitter, 10);
        rsp.setSuccess(true);
        rsp.put("replys", replys);
        return rsp;
    }

}
